<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/15
 * Time: 17:00
 */

namespace app\index\controller;
use app\index\model\OrderGiftCount;
use encryption\RSACrypt;
use think\Db;
use think\Request;
use app\index\model\Order as SelfModel;
use think\Exception;
use lib\Page;
use think\Cache;
class Order extends RSACrypt
{
    /**
     * 确认订单展示页
     * @param $member_id
     * @return array
     * 购物车没有运费
     * shipping 1商家配送 2到店自取
     */
    public function index($member_id,$address_id = 0,$type = 'cart',$shipping = 1,$pay_way = 1,$coupon_id = 0,$shop_id = 0)
    {
        //1默认地址 2,正常地址

        $cart_id_string = Request::instance()->param('cart_id_string');
        $radio_cart_list = Cart::index($member_id,$cart_id_string,$type);
        $order_price = 0;
        if(!empty($radio_cart_list)){
            foreach ($radio_cart_list['data'] as $k => &$v){
                $radio_cart_list['data'][$k]['coupon_id'] = 0;
                $radio_cart_list['data'][$k]['coupon_title'] = '';
                $radio_cart_list['data'][$k]['coupon_content'] = '';
                $radio_cart_list['data'][$k]['minus_price'] = '';
                $radio_cart_list['data'][$k]['total_price'] = '';
                $v['shop_freight'] = 0;
                $v['shop_count'] = 0;
                $v['shop_goods_amount'] = 0;
                $v['shop_goods_amount_coupon'] = 0;
                foreach ($v['list'] as $k1 => $v1){
                    $v['shop_freight'] += $v1['freight'];
                    if($shipping == 2){ //到店自取 运费为0
                        $v['shop_freight'] = 0;
                    }
                    $v['shop_goods_amount'] += $v1['number']*$v1['price'];
                    $v['shop_count'] += $v1['number'];
                }
                $v['shop_goods_amount_coupon'] = $v['shop_amount'] = $v['shop_freight']+ $v['shop_goods_amount'];
                if(!empty($shop_id) && !empty($coupon_id) && $v['shop_id'] == $shop_id){
                    $where_coupon['status'] = ['eq',1];
                    $where_coupon['start_time'] = ['elt',time()];
                    $where_coupon['end_time'] = ['gt',time()];
                    $where_coupon['member_id'] = ['eq',$member_id];
                    $where_coupon['id'] = ['eq',$coupon_id];
                    $member_coupon = Db::name('member_coupon')->fetchSql(false)->where($where_coupon)->find();
                    if($member_coupon){
                        $radio_cart_list['data'][$k]['coupon_id'] = $member_coupon['id'];
                        $radio_cart_list['data'][$k]['coupon_title'] = $member_coupon['title'];
                        $radio_cart_list['data'][$k]['coupon_content'] = $member_coupon['content'];
                        $radio_cart_list['data'][$k]['minus_price'] = $member_coupon['minus_price'];
                        $radio_cart_list['data'][$k]['total_price'] = $member_coupon['total_price'];
                        if($member_coupon['total_price'] > $v['shop_amount']){
                            return ['code' => 0 , 'message' => '优惠券金额不满足条件'];
                        }
                        $v['shop_amount'] -= $member_coupon['minus_price'];
                    }else{
                        return ['code' => 0 , 'message' => '此优惠卷过期或已使用或不存在'];
                    }
                }
                $v['explain'] = '';
//                $v['shipping'] = '配送方式';
//                $v['pay_way'] = '支付方式';
//                $v['bill'] = '是否开发票';
                $order_price += $v['shop_amount'];
            }
        }
        if(!empty($address_id)){
            //string(9) "index.php" 变量传空 返回index.php
            ///cart_id_string/2,3,7/member_id/1/address_id/
            $where['id'] = ['eq',$address_id];//只按照id来查
            $where['member_id'] = ['eq',$member_id];//按照id和会员member_id查。没有
        }else{
            $where['member_id'] = ['eq',$member_id];//按照id和会员member_id查。没有
        }
        $where_coupon['status'] = ['eq',1];
        $where_coupon['start_time'] = ['elt',time()];
        $where_coupon['end_time'] = ['gt',time()];
        $where_coupon['member_id'] = ['eq',$member_id];
        $where_coupon['coupon_id'] = ['eq',$coupon_id];

//        if($order_price < 0){
//            return ['code' => 0 , 'message' => '总订单金额有误'];
//        }
        if($order_price <= 0){
            $order_price = 0.01;
        }
        $member_address = Db::name('member_address')->order(['status' => 'asc'])->where($where)->find();
        if(is_null($member_address)) {
            $member_address['member_id'] = $member_id;
            $member_address['name'] = $member_address['phone'] = $member_address['area_info'] = $member_address['address'] = '';
        }
        $order_others = ['shipping' => $shipping , 'pay_way' => $pay_way , 'bill' => '','bill_data' => ['bill_type' => '','bill_header' => '','bill_content' => '','bill_phone' => '','bill_email' => '']];

        Cache::set('order_'.$member_id,'order_'.$member_id);
        return ['code' => 200, 'message' => '确认订单展示页','member_address' => $member_address,'radio_cart_list' => $radio_cart_list['data'],'order_amount' => $order_price,'order_others' => $order_others];
    }

    public function rsa(){
        $dat = $this->request();//解密后是数组
        writeLogOrder(json_encode($dat));
    }

    public function create_xiao()
    {

        if (!Request::instance()->isPost()) return ['code' => 400, 'message' => '请求方式错误'];
        // 获取参数
        $dat = $this->request();//解密后是数组

        //$dat = Request::instance()->param();
        writeLogOrder('xiao'.json_encode($dat));
        return ['code' => 400,'message' => json_decode($dat)];
        isset($dat['param']) ? $data = $dat['param'] : $data = $dat;
        $order_cache = Cache::get('order_'.$data['member_address']['member_id']);
        if(!empty($order_cache)){
            Cache::rm('order_'.$data['member_address']['member_id']);
        }else{
            return ['code' => 400, 'message' => '请勿重复下单'];
        }
        //$data= $dat['param'];
        writeLogOrder(json_encode($data));
        if (empty($data['member_address'])) return ['code' => 400, 'message' => '请至少选择一个地址'];
        if (!empty($data)) {
            Db::startTrans();
            try {
                $time = date('Y-m-d H:i:s', time());
                $member_id = $data['member_address']['member_id'];
                $username = Db::name('member')->where('id',$data['member_address']['member_id'])->value('username');
                $message = [];
                $order_number = $this->order_number();
                foreach ($data['radio_cart_list'] as $k => $v) {
                    if($data['order_others']['bill'] == 0){//开发票
                        $data['order_others']['bill'] = 2;
                    }
                    $shop_order_number = $this->shop_order_number($v['shop_id']);
                    $order[$v['shop_id']] = [
                        'finance_order' => set_finance_order($member_id),
                        'order_online' => 1,
                        'order_number' => $order_number,
                        'shop_order_number' => $shop_order_number,
                        'member_id' => $member_id,
                        'order_type' => 1,
                        'username' => $username,
                        'shop_id' => $v['shop_id'],
                        'amount' => $v['shop_amount'],
                        'order_price' => $v['shop_amount'],
                        'freight' => $v['shop_freight'],
                        'area_info' => $data['member_address']['area_info'],
                        'address' => $data['member_address']['address'],
                        'address_name' => $data['member_address']['name'],
                        'address_phone' => $data['member_address']['phone'],
                        'shipping' => $data['order_others']['shipping'],//货运方式
                        'pay_way' => $data['order_others']['pay_way'],
                        'explain' => $v['explain'],
                        'bill' => $data['order_others']['bill'],//是否开发票
                        'status' => 1,//未支付-待支付
                        'coupon_id' => $v['coupon_id'],
                        'coupon_title' => $v['coupon_title'],
                        'coupon_content' => $v['coupon_content'],
                        'coupon_price' => $v['minus_price'],
                        'coupon_totol_price' => $v['total_price'],
                        'create_time' => date('Y-m-d H:i:s', time())
                    ];
                    Db::name('member_coupon')->where('id',$v['coupon_id'])->setField('status',2);
                    if($data['order_others']['bill'] == 1){//开发票
                        $order[$v['shop_id']] = array_merge($order[$v['shop_id']],$data['order_others']['bill_data']);
                    }

                    $order_id = Db::name('order')->insertGetId($order[$v['shop_id']]);
                    Db::name('member_coupon')->where('id',$v['coupon_id'])->setField('status',2);
                    foreach ($v['list'] as $k1 => $v1) {
                        // 减少属性表库存
                        $inventory_attr = Db::name('shop_spec_attr')->where('shop_id',$v['shop_id'])->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->value('inventory');
                        if ($inventory_attr) {
                            if ($inventory_attr < $v1['number']) {
                                return ['code' => 401, 'message' => $v1['title'] . '(' . $v1['attribute'] . '),库存不足'];
                            }
                            Db::name('shop_spec_attr')->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->where('shop_id',$v['shop_id'])->setDec('inventory',$v1['number']);//门店库存
                            Db::name('shop_goods')->where('id',$v1['goods_id'])->setInc('sale_number',$v1['number']);//商品销量
                            Db::name('shop')->where('id',$v['shop_id'])->setInc('sale_number',$v1['number']);//门店销量
                        } //先验证
                        $order[$v['shop_id']]['shop_goods'][] = [
                            'member_id' => $member_id,
                            'order_id' => $order_id,
                            'order_number' => $order_number,
                            'shop_order_number' => $shop_order_number,
                            'shop_id' => $v1['shop_id'],
                            'goods_id' => $v1['goods_id'],
                            'title' => $v1['title'],
                            'number' => $v1['number'],
                            'attribute' => $v1['attribute'],
                            'freight' => $v1['freight'],
                            'price' => $v1['price'],
                            'per' => Db::name('shop_goods')->where('id',$v1['goods_id'])->value('per'),
                        ];
                    }
//                    $message[$v['shop_id']] = [
//                        'member_id' => $member_id,
//                        'order_number' => $shop_order_number,
//                        'title' => '您有一笔新的订单提交',
//                        'order_type' => '商品购买',
//                        'service_shop' => $v['shop_id'],
//                        'now_order_status' => 1,
//                        'shipping' => $data['order_others']['shipping'],
//                        'bill' => $data['order_others']['bill'],
//                        'explain' => $v['explain'],
//                        'order_content' => serialize($order[$v['shop_id']]['shop_goods']),
//                        'create_time' => $time,
//                    ];

                    Db::name('order_goods')->insertAll($order[$v['shop_id']]['shop_goods']);
                }//整个循环结束
                // Db::name('member_message')->insertAll($message);
                //清空当前会员购物车
                Db::name('cart')->where('member_id',$member_id)->delete();
                Db::commit();
                return ['code' => 200,'message' => '订单已生成','order_number' => $order_number];
            } catch (\Exception $e) {
                Db::rollback();
                return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
            }

        }else{
            return ['code' => 0 ,'message' => '数据错误'];
        }

    }

    public function j(){
       return ['a' => 1,'b' => 'c'];
    }

    public function create_xiao2()
    {

        if (!Request::instance()->isPost()) return ['code' => 400, 'message' => '请求方式错误'];
        // 获取参数
        //$dat = $this->request();//解密后是数组

        $dat = Request::instance()->param();

        writeLogOrder(($dat));
        isset($dat['param']) ? $data = $dat['param'] : $data = $dat;
        $data = json_decode($data,true);

        $order_cache = Cache::get('order_'.$data['member_address']['member_id']);
        if(!empty($order_cache)){
            Cache::rm('order_'.$data['member_address']['member_id']);
        }else{
            //return ['code' => 400, 'message' => '请勿重复下单'];
        }
        //$data= $dat['param'];
        writeLogOrder(json_encode($data));
        if (empty($data['member_address'])) return ['code' => 400, 'message' => '请至少选择一个地址'];
        if (!empty($data)) {
            Db::startTrans();
            try {
                $time = date('Y-m-d H:i:s', time());
                $member_id = $data['member_address']['member_id'];
                $username = Db::name('member')->where('id',$data['member_address']['member_id'])->value('username');
                $message = [];
                $order_number = $this->order_number();
                foreach ($data['radio_cart_list'] as $k => $v) {
                    if($data['order_others']['bill'] == 0){//开发票
                        $data['order_others']['bill'] = 2;
                    }
                    $shop_order_number = $this->shop_order_number($v['shop_id']);
                    $order[$v['shop_id']] = [
                        'device' => 2,
                        'finance_order' => set_finance_order($member_id),
                        'order_online' => 1,
                        'order_number' => $order_number,
                        'shop_order_number' => $shop_order_number,
                        'member_id' => $member_id,
                        'order_type' => 1,
                        'username' => $username,
                        'shop_id' => $v['shop_id'],
                        'amount' => $v['shop_amount'],
                        'order_price' => $v['shop_amount'],
                        'freight' => $v['shop_freight'],
                        'area_info' => $data['member_address']['area_info'],
                        'address' => $data['member_address']['address'],
                        'address_name' => $data['member_address']['name'],
                        'address_phone' => $data['member_address']['phone'],
                        'shipping' => $data['order_others']['shipping'],//货运方式
                        'pay_way' => $data['order_others']['pay_way'],
                        'explain' => $v['explain'],
                        'bill' => $data['order_others']['bill'],//是否开发票
                        'status' => 1,//未支付-待支付
                        'coupon_id' => $v['coupon_id'],
                        'coupon_title' => $v['coupon_title'],
                        'coupon_content' => $v['coupon_content'],
                        'coupon_price' => $v['minus_price'],
                        'coupon_totol_price' => $v['total_price'],
                        'create_time' => date('Y-m-d H:i:s', time())
                    ];
                    Db::name('member_coupon')->where('id',$v['coupon_id'])->setField('status',2);
                    if($data['order_others']['bill'] == 1){//开发票
                        $order[$v['shop_id']] = array_merge($order[$v['shop_id']],$data['order_others']['bill_data']);
                    }

                    $order_id = Db::name('order')->insertGetId($order[$v['shop_id']]);
                    Db::name('member_coupon')->where('id',$v['coupon_id'])->setField('status',2);
                    foreach ($v['list'] as $k1 => $v1) {
                        // 减少属性表库存
                        $inventory_attr = Db::name('shop_spec_attr')->where('shop_id',$v['shop_id'])->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->value('inventory');
                        if ($inventory_attr) {
                            if ($inventory_attr < $v1['number']) {
                                return ['code' => 401, 'message' => $v1['title'] . '(' . $v1['attribute'] . '),库存不足'];
                            }
                            Db::name('shop_spec_attr')->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->where('shop_id',$v['shop_id'])->setDec('inventory',$v1['number']);//门店库存
                            Db::name('shop_goods')->where('id',$v1['goods_id'])->setInc('sale_number',$v1['number']);//商品销量
                            Db::name('shop')->where('id',$v['shop_id'])->setInc('sale_number',$v1['number']);//门店销量
                        } //先验证
                        $order[$v['shop_id']]['shop_goods'][] = [
                            'member_id' => $member_id,
                            'order_id' => $order_id,
                            'order_number' => $order_number,
                            'shop_order_number' => $shop_order_number,
                            'shop_id' => $v1['shop_id'],
                            'goods_id' => $v1['goods_id'],
                            'title' => $v1['title'],
                            'number' => $v1['number'],
                            'attribute' => $v1['attribute'],
                            'freight' => $v1['freight'],
                            'price' => $v1['price'],
                            'per' => Db::name('shop_goods')->where('id',$v1['goods_id'])->value('per'),
                        ];
                    }
//                    $message[$v['shop_id']] = [
//                        'member_id' => $member_id,
//                        'order_number' => $shop_order_number,
//                        'title' => '您有一笔新的订单提交',
//                        'order_type' => '商品购买',
//                        'service_shop' => $v['shop_id'],
//                        'now_order_status' => 1,
//                        'shipping' => $data['order_others']['shipping'],
//                        'bill' => $data['order_others']['bill'],
//                        'explain' => $v['explain'],
//                        'order_content' => serialize($order[$v['shop_id']]['shop_goods']),
//                        'create_time' => $time,
//                    ];
                    Db::name('order_goods')->insertAll($order[$v['shop_id']]['shop_goods']);
                }//整个循环结束
                // Db::name('member_message')->insertAll($message);
                //清空当前会员购物车
                Db::name('cart')->where('member_id',$member_id)->delete();
                Db::commit();
                return ['code' => 200,'message' => '订单已生成','order_number' => $order_number];
            } catch (\Exception $e) {
                Db::rollback();
                return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
            }

        }else{
            return ['code' => 0 ,'message' => '数据错误'];
        }

    }

    /*
 * 生成订单号码
 */
    protected function order_number()
    {
        //订单号码主体（YYYYMMDDHHIISSNNNNNNNN）
        $order_id_main = date('YmdHis') . rand(10000000, 99999999);
        //订单号码主体长度
        $order_id_len = strlen($order_id_main);
        $order_id_sum = 0;
        for ($i = 0; $i < $order_id_len; $i++) {
            $order_id_sum += (int)(substr($order_id_main, $i, 1));
        }
        //唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
        $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);

        return $order_id;
    }

    /*
     * 生成店铺订单号码
     */
    protected function shop_order_number($shop_id)
    {
        return date('YmdHis', time())
            . mt_rand(10, 99)
            . sprintf('%010d', time() - 946656000)
            . sprintf('%03d', (float)microtime() * 1000)
            . sprintf('%03d', (int)$shop_id % 1000);

    }

    public function i(){
        $header = Request::instance()->header();
        halt($header);
    }
    /**
     * 生成订单
     */
    public function create()
    {

        if (!Request::instance()->isPost()) return ['code' => 400, 'message' => '请求方式错误'];
        // 获取参数
        $i = Request::instance()->param();

        $dat = $this->request();//解密后是数组
        writeLogOrder('解密后的数据:'.json_encode($dat));
        isset($dat['param']) ? $data = $dat['param'] : $data = $dat;
        $order_cache = Cache::get('order_'.$data['member_address']['member_id']);
        if(!empty($order_cache)){
            Cache::rm('order_'.$data['member_address']['member_id']);
        }else{
            return ['code' => 400, 'message' => '请勿重复下单'];
        }
        //$data= $dat['param'];
        writeLogOrder(json_encode($data));
        if (empty($data['member_address'])) return ['code' => 400, 'message' => '请至少选择一个地址'];
        if (!empty($data)) {
            Db::startTrans();
            try {
                $time = date('Y-m-d H:i:s', time());
                $member_id = $data['member_address']['member_id'];
                $username = Db::name('member')->where('id',$data['member_address']['member_id'])->value('username');
                $message = [];
                $order_number = $this->order_number();
                foreach ($data['radio_cart_list'] as $k => $v) {
                    if($data['order_others']['bill'] == 0){//开发票
                        $data['order_others']['bill'] = 2;
                    }
                    $shop_order_number = $this->shop_order_number($v['shop_id']);
                    $order[$v['shop_id']] = [
                        'finance_order' => set_finance_order($member_id),
                        'order_online' => 1,
                        'order_number' => $order_number,
                        'shop_order_number' => $shop_order_number,
                        'member_id' => $member_id,
                        'order_type' => 1,
                        'username' => $username,
                        'shop_id' => $v['shop_id'],
                        'amount' => $v['shop_amount'],
                        'order_price' => $v['shop_amount'],
                        'freight' => $v['shop_freight'],
                        'area_info' => $data['member_address']['area_info'],
                        'address' => $data['member_address']['address'],
                        'address_name' => $data['member_address']['name'],
                        'address_phone' => $data['member_address']['phone'],
                        'shipping' => $data['order_others']['shipping'],//货运方式
                        'pay_way' => $data['order_others']['pay_way'],
                        'explain' => $v['explain'],
                        'bill' => $data['order_others']['bill'],//是否开发票
                        'status' => 1,//未支付-待支付
                        'coupon_id' => $v['coupon_id'],
                        'coupon_title' => $v['coupon_title'],
                        'coupon_content' => $v['coupon_content'],
                        'coupon_price' => $v['minus_price'],
                        'coupon_totol_price' => $v['total_price'],
                        'create_time' => date('Y-m-d H:i:s', time())
                    ];
                    Db::name('member_coupon')->where('id',$v['coupon_id'])->setField('status',2);
                    if($data['order_others']['bill'] == 1){//开发票
                        $order[$v['shop_id']] = array_merge($order[$v['shop_id']],$data['order_others']['bill_data']);
                    }

                    $order_id = Db::name('order')->insertGetId($order[$v['shop_id']]);
                    Db::name('member_coupon')->where('id',$v['coupon_id'])->setField('status',2);
                    foreach ($v['list'] as $k1 => $v1) {
                        // 减少属性表库存
                        $inventory_attr = Db::name('shop_spec_attr')->where('shop_id',$v['shop_id'])->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->value('inventory');
//                        if ($inventory_attr) {
//                            if ($inventory_attr < $v1['number']) {
//                                return ['code' => 401, 'message' => $v1['title'] . '(' . $v1['attribute'] . '),库存不足'];
//                            }
//                            Db::name('shop_spec_attr')->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->where('shop_id',$v['shop_id'])->setDec('inventory',$v1['number']);//门店库存
//                            Db::name('shop_goods')->where('id',$v1['goods_id'])->setInc('sale_number',$v1['number']);//商品销量
//                            Db::name('shop')->where('id',$v['shop_id'])->setInc('sale_number',$v1['number']);//门店销量
//                        } //先验证
                        $order[$v['shop_id']]['shop_goods'][] = [
                            'member_id' => $member_id,
                            'order_id' => $order_id,
                            'order_number' => $order_number,
                            'shop_order_number' => $shop_order_number,
                            'shop_id' => $v1['shop_id'],
                            'goods_id' => $v1['goods_id'],
                            'title' => $v1['title'],
                            'number' => $v1['number'],
                            'attribute' => $v1['attribute'],
                            'freight' => $v1['freight'],
                            'price' => $v1['price'],
                            'per' => Db::name('shop_goods')->where('id',$v1['goods_id'])->value('per'),
                        ];
                    }
//                    $message[$v['shop_id']] = [
//                        'member_id' => $member_id,
//                        'order_number' => $shop_order_number,
//                        'title' => '您有一笔新的订单提交',
//                        'order_type' => '商品购买',
//                        'service_shop' => $v['shop_id'],
//                        'now_order_status' => 1,
//                        'shipping' => $data['order_others']['shipping'],
//                        'bill' => $data['order_others']['bill'],
//                        'explain' => $v['explain'],
//                        'order_content' => serialize($order[$v['shop_id']]['shop_goods']),
//                        'create_time' => $time,
//                    ];
                    Db::name('order_goods')->insertAll($order[$v['shop_id']]['shop_goods']);
                }//整个循环结束
                // Db::name('member_message')->insertAll($message);
                //清空当前会员购物车
                Db::name('cart')->where('member_id',$member_id)->delete();
                Db::commit();
                return ['code' => 200,'message' => '订单已生成','order_number' => $order_number];
            } catch (\Exception $e) {
                Db::rollback();
                return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
            }

        }else{
            return ['code' => 0 ,'message' => '数据错误'];
        }

    }


//    public function my($member_id,$status = 0)
//    {
//        $where['member_id'] = ['eq',$member_id];
//        if(!empty($status)) $where['status'] = ['eq',$status];
//        $data = (new SelfModel())->with(['Shop' => function ($query) {
//            $query->field('id,shop_title');
//        },'OrderGoods' => function ($query) {
//            $query->field('order_id,goods_id,title,attribute,price,number,thumb');
//        }])->where($where)->order(['create_time' => 'desc'])->select();
//        foreach ($data as $k => $v){
//            //dump($v);
//        }
//        return ['code' => 200 ,'message' => '我的订单列表','data' => $data];
//    }

    /**
     * @param $member_id
     * @param int $status
     * @param int $is_evaluate
     * @return array
     * 我的订单
     */
    public function my($member_id, $status = 0,$shop_order_number = 0,$title = '')
    {
        try {
            $where_title = [];
            $where['o.member_id'] = ['eq', $member_id];
            $where['o.status'] = ['not in', '50,20'];
            if (!empty($status)) $where['o.status'] = ['eq', $status];
            if (!empty($status) && ($status == 2 || $status == 3)) {
                $where['o.status'] = ['in', '2,3'];
            }
            if (!empty($status) && ($status == 10 || $status == 11 || $status == 12)) {
                $where['o.status'] = ['in', '10,11,12'];
            }
            if (!empty($title)) $where['og.title|o.gift_title|o.mc_title'] = ['like', '%' . $title . '%'];
            if (!empty($is_evaluate)) $where['og.is_evaluate'] = ['eq', $is_evaluate];
            if (!empty($shop_order_number)) $where['o.shop_order_number|o.order_number|og.title'] = ['like', '%' . $shop_order_number . '%'];
//        $data = Db::name('Order')->alias('o')
//            ->join('OrderGoods og', 'o.id = og.order_id')
//            ->join('ShopGoods g', 'g.id = og.goods_id')
//            ->join('Shop s', 's.id = o.shop_id')
//            ->field('o.shop_id,o.order_number,o.shop_order_number,s.shop_title,g.title,og.goods_id,og.price,og.number,og.attribute,g.thumb_img,og.is_evaluate,o.status')
//            ->where($where)
//            ->select();

            $order = (new \app\index\model\Order())->alias('o')
                ->field('o.gift_title,o.t_type,o.gift_total_count,o.id,o.member_id,o.shop_id,o.amount,o.freight,o.service_type_id,o.order_type,o.single_parent_id,o.single_id,o.status,o.order_number,o.shop_order_number,s.shop_title,t_type,mc_type,mc_title,mc_day,mc_start_time,mc_end_time,system_card_id,mc_count,mc_single_pic')
                ->join('Shop s', 's.id = o.shop_id', 'left')
                ->join('order_goods og', 'og.order_id = o.id', 'left')
                ->where($where)
                ->where('o.is_delete',1)
                ->where('o.auding_status',2)
                ->order('id', 'desc')
                ->group('o.id')
                ->paginate(600)->each(function ($item, $key) use ($shop_order_number, $where_title) {
                    switch ($item['order_type']) {
                        case 1;
                            //$item->order_number = $item->shop_order_number;
                            $item['goods_list'] = Db::name('order_goods')->alias('og')->join('shop_goods g', 'og.goods_id = g.id')->field('og.id,og.goods_id,og.title,og.price,og.number,og.attribute,g.thumb_img')->where('order_id', $item['id'])->select();
                            break;
                        case 2;
                            if ($item->shop_id == 0) {
                                $shop_title = '目前还没有指定门店';
                            } else {
                                $shop_title = $item->shop_title;
                            }
                            $item->shop_title = $shop_title;
                            switch ($item['service_type_id']) {
                                case 1;
                                    $data = Db::name('order_service os')->join('order o', 'o.id = os.order_id')->join('single s', 'o.single_id = s.id', 'left')->field('s.pic_url,os.type_id_str,phone,protecteder,protecter_back,car_face,car_back')->where('os.order_id', $item['id'])->select()->toArray();
                                    $data[0]['single_title'] = Db::name('single')->where('id', $item['single_id'])->value('cat_name');
                                    $data[0]['single_parent_title'] = Db::name('single')->where('id', $item['single_parent_id'])->value('cat_name');
                                    $item['service_list'] = $data;
                                    break;
                                case 2;
                                    $data = Db::name('order_service os')->join('order o', 'o.id = os.order_id')->join('single s', 'o.single_id = s.id', 'left')->field('os.type_id_str,s.pic_url,os.type_id_str,phone,protecteder,protecter_back,car_face,car_back')->where('os.order_id', $item['id'])->select()->toArray();
                                    $data[0]['single_title'] = Db::name('single')->where('id', $item['single_id'])->value('cat_name');
                                    $data[0]['single_parent_title'] = Db::name('single')->where('id', $item['single_parent_id'])->value('cat_name');
                                    $item['service_list'] = $data;
                                    break;
                                case 3;
                                    $data = Db::name('order_service os')->join('order o', 'o.id = os.order_id')->join('single s', 'o.single_id = s.id', 'left')->field('s.pic_url,os.type_id_str,phone,protecteder,protecter_back,car_face,car_back')->where('os.order_id', $item['id'])->select()->toArray();
                                    $data[0]['single_title'] = Db::name('single')->where('id', $item['single_id'])->value('cat_name');
                                    $data[0]['single_parent_title'] = Db::name('single')->where('id', $item['single_parent_id'])->value('cat_name');
                                    $data[0]['pic_url'] = Db::name('single')->where('id', $item['single_id'])->value('pic_url');
                                    $data[0]['phone'] = '';
                                    $data[0]['protecteder'] = '';
                                    $data[0]['protecter_back'] = '';
                                    $data[0]['car_face'] = '';
                                    $data[0]['car_back'] = '';
                                    $data[0]['type_id_str'] = '';
                                    $item['service_list'] = $data;
                                    break;
                                case 4;
                                    $data = Db::name('order_service os')->join('order o', 'o.id = os.order_id')->join('single s', 'o.single_id = s.id', 'left')->field('s.pic_url,os.type_id_str,phone,protecteder,protecter_back,car_face,car_back')->where('os.order_id', $item['id'])->select()->toArray();
                                    $data[0]['single_title'] = Db::name('single')->where('id', $item['single_id'])->value('cat_name');
                                    $data[0]['single_parent_title'] = Db::name('single')->where('id', $item['single_parent_id'])->value('cat_name');
                                    $data[0]['pic_url'] = Db::name('single')->where('id', $item['single_id'])->value('pic_url');
                                    $data[0]['phone'] = '';
                                    $data[0]['protecteder'] = '';
                                    $data[0]['protecter_back'] = '';
                                    $data[0]['car_face'] = '';
                                    $data[0]['car_back'] = '';
                                    $data[0]['type_id_str'] = '';
                                    $item['service_list'] = $data;
                                    break;
                            }
                            break;
                        case 3;
                            if($item['t_type'] == 1){
                                $item->shop_title = '礼包订单';
                                $item['gift_list'] = Db::name('order o')->join('gift g', 'o.gift_id = g.id')->field('g.id,g.single_pic,o.gift_title,o.gift_describe,o.sale_price,o.gift_number')->where('o.id', $item['id'])->select();
                                $data = Db::name('order_gift_count')
                                    ->alias('ogc')
                                    ->join('order o','ogc.order_id = o.id')
                                    ->where('o.id',$item['id'])
                                    ->join('single s','ogc.single_content = s.id','left')
                                    ->field('cat_name,gift_count single_count')
                                    ->select();
                                //总次数+分组 = 分类总次数 礼包只能去一个门店使用 不会有并发
                                $str = '';
                                if (!empty($data)) {
                                    foreach ($data as $k => $v) {
                                        $str .= $v['cat_name'] . ':' . $v['single_count'] . '次' . ' ';
                                    }

                                }
                                $item['gift_residue_number'] = $str;
                            }
                            //会员卡...
                            if($item['t_type'] == 2){
                                //$item->shop_title = '礼包订单(会员卡-年卡)';
                                if ($item->mc_type == 1) {
                                    $item->shop_title = '礼包订单(会员卡-年卡)';
                                    $item['gift_residue_number'] = '';
                                }

                                if ($item['mc_type'] == 2) {
                                    $data = Db::name('order_mc_count')
                                        ->alias('ogc')
                                        ->join('order o','ogc.order_id = o.id')
                                        ->where('o.id',$item['id'])
                                        ->join('single s','ogc.single_content = s.id','left')
                                        ->field('cat_name,gift_count single_count')
                                        ->select();
                                    //总次数+分组 = 分类总次数 礼包只能去一个门店使用 不会有并发
                                    $str = '';
                                    if (!empty($data)) {
                                        foreach ($data as $k => $v) {
                                            $str .= $v['cat_name'] . ':' . $v['single_count'] . '次' . ' ';
                                        }

                                    };
                                    $item->shop_title = '礼包订单(会员卡-次卡)';
                                    $item['gift_residue_number'] = '使用次数:' . $str;
                                }
                                $single_pic = db('system_card')->where('id',$item->system_card_id)->value('single_pic');
                                $item['gift_list'] = [
                                    ['id' => $item->system_card_id,'single_pic' => $single_pic,'gift_title' => $item->mc_title,'gift_describe' => 1,'sale_price' => $item->amount,'gift_number' => 1]
                                ];
                            }
                            break;
                    }
                });

            $ret = [];
//        foreach ($order as $k => $v) {
//            $ret[$v['shop_order_number']]['shop_title'] = $v['shop_title'];
//            $ret[$v['shop_order_number']]['list'][] = $v;
//            $ret[$v['shop_order_number']]['status'] = $v['status'];
//        }
//        $Page = new Page(10, 2, false);
//        $pageHtml  = $Page->show(__FUNCTION__);// 分页显示输出
//        return $pageHtml;
            return ['code' => 200, 'message' => '我的订单列表', 'data' => ($order)];
        } catch (Exception $e) {
            return ['code' => $e->getCode(), 'message' => $e->getMessage(),'line' => $e->getLine()];
        }
    }

    public function text()
    {
        $data = Db::name('shop_assignor_records sar')
            ->join('shop', 'shop.id = sar.shop_id', 'left')
            ->join('shop_assignor sa', 'sar.assignor_id = sa.id', 'left')
            ->join('single s', 'sar.single_id = s.id', 'left')
            ->join('order o', 'o.id = sar.order_id')
            ->where('o.order_number|o.shop_order_number', '201812131158144966023527')
            ->group('sar.single_id')
            ->where('sar.is_agree',2)
            ->field('s.cat_name,count(sar.single_id) single_count')
            ->select();
        $str = '';
        if(!empty($data)){
            foreach ($data as $k => $v){
                $str .= $v['cat_name'].':'.$v['single_count'].'次'.' ';
            }

        }
        return $str;
    }


    /**
     * @param $shop_order_number
     * @param $member_id
     * @return array
     * 订单详情
     */
    public function view($shop_order_number,$member_id)
    {
        $order = Db::name('order')
            ->where('member_id',$member_id)
            ->where('order_number|shop_order_number',$shop_order_number)
            ->find();
        if($order['single_parent_id'] == 1){
            $order['order_price'] += $order['sj'];
        }

        if($order['order_type'] == 1){ //商品订单
            if($order['shop_id'] != 0){
                $order['shop_title'] = Db::name('shop')->where('id',$order['shop_id'])->value('shop_title');
            }

            $list = Db::name('order_goods')->field('id,goods_id,title,attribute,price,number')->where('order_id',$order['id'])->select()->toArray();
            foreach ($list as $k =>$v){
                $list[$k]['thumb_url'] = Db::name('shop_goods')->where('id',$v['goods_id'])->value('thumb_img');
            }
            $order['list'] = $list;

        }
        if($order['order_type'] == 3 && $order['t_type'] == 1){ //礼包订单
            $list = Db::name('order o')
                ->join('gift g','o.gift_id = g.id')
                ->field('o.gift_id id,o.gift_title title,o.gift_describe attribute,o.sale_price price,o.gift_number number,g.single_pic thumb_url')
                ->where('o.member_id',$member_id)
                ->where('o.shop_order_number|o.order_number',$shop_order_number)
                ->select();

            $order['shop_title'] = '礼包订单';
            $order['list'] = $list;
            $order['records'] = Db::name('shop_assignor_records sar')
                ->join('shop', 'shop.id = sar.shop_id', 'left')
                ->join('shop_assignor sa', 'sar.assignor_id = sa.id', 'left')
                ->join('order o', 'o.id = sar.order_id')
                ->join('single s','sar.single_id = s.id')
                ->where('o.order_number|o.shop_order_number', $shop_order_number)
                ->field('sar.*,shop.shop_title,s.cat_name name,cat_name service_content')
                ->where('sar.is_agree',2)
                ->group('sar.create_time')
                ->order('id','desc')
                ->paginate(6);
        }
        if($order['order_type'] == 3 && $order['t_type'] == 2){  //会员卡订单
            $list = Db::name('order o')
                ->field('o.gift_id id,o.mc_title title,o.mc_title attribute,o.amount price,o.gift_number number,o.mc_single_pic thumb_url')
                ->where('o.member_id',$member_id)
                ->where('o.shop_order_number|o.order_number',$shop_order_number)
                ->select();

            $order['shop_title'] = '礼包订单(会员卡)';
            $order['list'] = $list;
            $order['records'] = [];
        }
        return ['code' => 200 ,'message' => '订单详情','data' => $order];
    }

    /**
     * @param $shop_assignor_records_id
     * @return array
     * 苹果特意使用 二级分类
     */
    public function records($shop_order_number)
    {
        $t_type = Db::name('order')->where('order_number|shop_order_number|id',$shop_order_number)->value('t_type');
        if($t_type == 1){  //1Libao
            $data = Db::name('shop_assignor_records sar')
                ->join('shop', 'shop.id = sar.shop_id', 'left')
                ->join('shop_assignor sa', 'sar.assignor_id = sa.id', 'left')
                ->join('order o', 'o.id = sar.order_id')
                ->join('single s','sar.assignor_content = s.id')
                ->where('o.order_number|o.shop_order_number|o.id', $shop_order_number)
                ->field('sar.*,left(sar.create_time,10) create_time,shop.shop_title,s.cat_name name,cat_name service_content')
                ->where('sar.is_agree',2)
                ->where('assignor_content','neq',9999)
//            ->group('sar.create_time')
                ->order('id','desc')
                ->paginate(6);
        }
        if($t_type == 2) {
            $order_id = Db::name('order')->where('order_number|shop_order_number',$shop_order_number)->value('id');

            $data = Db::name('member_card_records sar')
                ->join('shop', 'shop.id = sar.shop_id', 'left')
                ->join('shop_assignor sa', 'sar.shop_assignor_id = sa.id', 'left')
                ->join('order o', 'o.id = sar.order_id')
                ->join('single s', 'sar.service_content = s.id')
                ->where('o.order_number|o.shop_order_number|o.id', $shop_order_number)
                ->field('sar.*,left(sar.create_time,10) create_time,shop.shop_title,s.cat_name name,cat_name service_content')
                ->where('sar.is_agree', 2)
//            ->group('sar.create_time')
                ->order('id', 'desc')
                ->paginate(6);
        }
        return ['code' => 200, 'message' => '礼包纪录', 'data' => $data];
    }

    public function emp123($shop_assignor_records_id){

        $data = Db::name('shop_assignor_records sar')
            ->join('order o','o.id = sar.order_id','left')
            ->join('shop', 'shop.id = sar.shop_id', 'left')
            ->join('shop_assignor sa', 'sar.assignor_id = sa.id', 'left')
            ->join('single s','sar.single_id = s.id','left')
            ->where('sar.id', $shop_assignor_records_id)
            ->field('sar.*,shop.shop_title,sa.name,o.order_number,o.order_type,s.cat_name assignor_content')
            ->find();

        if(empty($data)){

            $this->emp($shop_assignor_records_id-1);
        }else{

            return $data;
        }
    }



    /**
     * 礼包消息 单条
     */
    public function gift_view($shop_assignor_records_id)
    {

        $is_click = Db::name('member_message')->where('order_number',$shop_assignor_records_id)->value('is_click');
        if($is_click == 1){
            Db::name('member_message')->where('order_number',$shop_assignor_records_id)->setField('is_click','2');
        }
//        $data = emp($shop_assignor_records_id);
//            halt($data);
        $data = Db::name('shop_assignor_records sar')
            ->join('order o','o.id = sar.order_id','left')
            ->join('shop', 'shop.id = sar.shop_id', 'left')
            ->join('shop_assignor sa', 'sar.assignor_id = sa.id', 'left')
            ->join('single s','sar.single_id = s.id','left')
            ->where('sar.id', $shop_assignor_records_id)
			
            ->field('sar.*,shop.shop_title,sa.name,o.order_number,o.order_type,s.cat_name assignor_content,o.car_number')
            ->find();
//        if(empty($data)){
//            $shop_assignor_records_id = $shop_assignor_records_id-1;
//            $data = Db::name('shop_assignor_records sar')
//                ->join('order o','o.id = sar.order_id','left')
//                ->join('shop', 'shop.id = sar.shop_id', 'left')
//                ->join('shop_assignor sa', 'sar.assignor_id = sa.id', 'left')
//                ->join('single s','sar.single_id = s.id','left')
//                ->where('sar.id', $shop_assignor_records_id)
//                ->field('sar.*,shop.shop_title,sa.name,o.order_number,o.order_type,s.cat_name assignor_content')
//                ->find();
//        }
        $order_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('order_id');

        $shop_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('shop_id');
        $create_time = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('create_time');
        $select = Db::name('shop_assignor_records sar')
            ->where('order_id', $order_id)
            ->where('shop_id', $shop_id)
            ->where('sar.create_time', $create_time)
            ->join('single s','sar.assignor_content = s.id')
            ->group('cat_name')
            ->where('assignor_content','neq',9999)
            ->column('cat_name');
        $assignor_content_column = Db::name('shop_assignor_records sar')
            ->where('order_id', $order_id)
            ->where('shop_id', $shop_id)
            ->where('sar.create_time', $create_time)
            ->join('single s','sar.assignor_content = s.id')
            ->group('assignor_content')
            ->where('assignor_content','neq',9999)
            ->column('assignor_content');

        //$assignor_content_text = Db::name('single')->where('id','in',$select)->column('cat_name');
        /**
         * 商品判断 类型
         */
        $assignor_content_array = implode(',',$select);
        $data['assignor_content'] = $assignor_content_array;
        $title_array = Db::name('order_goods')->where('order_id',$order_id)->column('title');
        $goods_title = implode(',',$title_array);
        if($data['order_type'] == 1){
            $data['assignor_content'] = $goods_title;
        }
        if(is_null($data['shop_title'])) $data['shop_title'] = Db::name('shop')->where('id',$shop_id)->value('shop_title');

        $select2 = (new OrderGiftCount())->where('single_content','in',$assignor_content_column)->where('order_id',$order_id)->select();//field('id,single_content,gift_count,gift_origin_count')->
        foreach ($select2 as &$v){
            $v['single_content'] = Db::name('single')->where('id',$v['single_content'])->value('cat_name');
        }
        $data['order_gift_count'] = $select2;
        return ['code' => 200, 'message' => '礼包确认使用详情', 'data' => $data,'assignor_content_column' => $assignor_content_column];
    }

    /**
     * 会员卡消息 单条
     */
    public function member_card_view($shop_assignor_records_id)
    {

        $is_click = Db::name('member_message')->where('order_number',$shop_assignor_records_id)->value('is_click');
        if($is_click == 1){
            Db::name('member_message')->where('order_number',$shop_assignor_records_id)->setField('is_click','2');
        }
        $data = Db::name('member_card_records mcr')
            ->join('shop s', 's.id = mcr.shop_id', 'left')
            ->where('mcr.id', $shop_assignor_records_id)
            ->field('mcr.id,mcr.single_title,s.shop_title,mcr.create_time,mcr.is_agree,mcr.shop_id,mcr.card_id,mcr.member_id')
            ->find();
        $single_content = Db::name('member_card_records mcr')
            ->join('shop s', 's.id = mcr.shop_id', 'left')
            ->join('single se','mcr.service_content = se.id','left')
            ->where('mcr.create_time',$data['create_time'])
            ->where('mcr.shop_id',$data['shop_id'])
            ->where('mcr.card_id',$data['card_id'])
            ->where('mcr.member_id',$data['member_id'])
            ->field('mcr.id,mcr.single_title,s.shop_title,mcr.create_time,mcr.is_agree,se.cat_name')

            ->column('cat_name');

        $data['single_title'] = implode(',',$single_content);
        return ['code' => 200, 'message' => '会员卡消息单条', 'data' => $data];
    }

    /**
     * @param $shop_assignor_records_id
     * @return array
     * 会员卡前台同意生效
     */
    public function member_card_agree($shop_assignor_records_id)
    {
        Db::startTrans();
        try {
            $is_agree = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('is_agree');

            if ($is_agree == 2) {
                return ['code' => 400, 'message' => '已同意过'];
            }
            $create_time = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('create_time');
            //$single_id = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('single_id');
            $shop_id = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('shop_id');
            $card_id = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('card_id');
            $member_id = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('member_id');
            $card_type = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('card_type');
            $order_id = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('order_id');
            if ($card_type == 12) {
                //            Db::name('member_card')->where('id', $card_id)->setDec('minus_count');//次卡减去次数
                //            Db::name('member_card_records')
                //                //->where('single_id', $single_id)
                //                ->where('shop_id', $shop_id)
                //                ->where('create_time', $create_time)
                //->setDec('minus_count');
                $select = Db::name('member_card_records')
                    ->where('order_id', $order_id)
                    ->where('shop_id', $shop_id)
                    ->where('create_time', $create_time)
                    ->group('service_content')
                    ->select();
                foreach ($select as $v) {
                    $g_ret = Db::name('order_mc_count')
                        ->where('order_id', $order_id)
                        ->where('single_content', $v['service_content'])
                        ->setDec('gift_count', 1);//订单礼包次数
                }
                //            $now_member =  Db::name('member_card_records')
                //                ->where('shop_id', $shop_id)
                //                ->where('member_id', $member_id)
                //                ->where('create_time', $create_time)
                //                ->select()->toArray();
                $i = 1;
                //            foreach (array_reverse($now_member) as $v){
                //                //Db::name('member_card')->where('id', $card_id)->setDec('minus_count');//会员卡次卡减去次数
                //                //Db::name('member_card_records')->where('id',$v['id'])->setDec('minus_count',$i);
                //                $i++;
                //            }

            }
            //后修改 当前门店 当前时间 当前订单 当前分类 都 修改
            $ret = Db::name('member_card_records')
                //->where('single_id', $single_id)
                ->where('shop_id', $shop_id)
                ->where('create_time', $create_time)
                ->where('member_id', $member_id)
                ->setField('is_agree', 2);
            if ($ret !== false) {
                //$shop_assignor_records[$i]['minus_count'] = $minus_count - 1;
                //Db::name('member_card')->where('id', $param['card_id'])->setDec('minus_count');//次卡减去次数
                Db::commit();
                return ['code' => 200, 'message' => '确认成功'];
            }

        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(), 'message' => '确认失败'];
        }
        return ['code' => 400, 'message' => '确认失败'];
    }

    /**
     * @param $shop_assignor_records_id
     * @return array
     * 前台同意才算生效 之前
     */
    public function gift_agree2($shop_assignor_records_id)
    {

        $order_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('order_id');
        $single_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('single_id');
        $shop_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('shop_id');
        $create_time = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('create_time');
        $r = Db::name('order_gift_count')->where('order_id', $order_id)->where('single_id', $single_id)->setDec('gift_count');//订单礼包次数
        //后修改 当前门店 当前时间 当前订单 当前分类 都 修改
        $ret = Db::name('shop_assignor_records')
            ->where('order_id', $order_id)
            ->where('single_id', $single_id)
            ->where('shop_id', $shop_id)
            ->where('create_time', $create_time)
            ->setField('is_agree', 2);
        if ($ret !== false) {
            return ['code' => 200, 'message' => '确认成功'];
        }
        return ['code' => 400, 'message' => '确认失败'];
    }

    /**
     * @param $shop_assignor_records_id
     * @return array
     * 礼包同意
     */
    public function gift_agree($shop_assignor_records_id)
    {

        $is_agree = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('is_agree');

        if($is_agree == 2){
            return ['code' => 400, 'message' => '已同意过'];
        }
        Db::startTrans();
        try {
            $order_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('order_id');
            $shop_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('shop_id');
            $create_time = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('create_time');
            $select = Db::name('shop_assignor_records')
                ->where('order_id', $order_id)
                ->where('shop_id', $shop_id)
                ->where('create_time', $create_time)
                ->group('assignor_content')
                ->select();
            //循环减当前类的次数
//            foreach ($select as $v) {
//                $g_ret = Db::name('order_gift_count')
//                    ->where('order_id', $order_id)
//                    ->where('single_content', $v['assignor_content'])
//                    ->setDec('gift_count', 1);//订单礼包次数
//            }
            //$single_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('single_id');
            //$r = Db::name('order_gift_count')->where('order_id', $order_id)->where('single_id', $single_id)->setDec('gift_count');//订单礼包次数
            //后修改 当前门店 当前时间 当前订单 当前分类 都 修改
			//查询批次
			$pici = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('pici');
            $ret = Db::name('shop_assignor_records')
                ->where('order_id', $order_id)
				->where('pici', $pici)
                //            ->where('single_id', $single_id)
                //->where('shop_id', $shop_id)
				
                //->where('create_time', $create_time)
                ->setField('is_agree', 2);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage()];
        }
        if ($ret !== false) {
            return ['code' => 200, 'message' => '确认成功'];
        }
        return ['code' => 400, 'message' => '确认失败'];
    }


    public function gift_agree_houtai($shop_assignor_records_id)
    {

        $is_agree = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('is_agree');

        if($is_agree == 2){
            return ['code' => 400, 'message' => '已同意过'];
        }
        Db::startTrans();
        try {
            $order_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('order_id');
            $shop_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('shop_id');
            $create_time = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('create_time');
            $select = Db::name('shop_assignor_records')
                ->where('order_id', $order_id)
                ->where('shop_id', $shop_id)
                ->where('create_time', $create_time)
                ->group('assignor_content')
                ->select();
            //循环减当前类的次数
            foreach ($select as $v) {
                $g_ret = Db::name('order_gift_count')
                    ->where('order_id', $order_id)
                    ->where('single_content', $v['assignor_content'])
                    ->setDec('gift_count', 1);//订单礼包次数
            }
            //$single_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('single_id');
            //$r = Db::name('order_gift_count')->where('order_id', $order_id)->where('single_id', $single_id)->setDec('gift_count');//订单礼包次数
            //后修改 当前门店 当前时间 当前订单 当前分类 都 修改
//            $ret = Db::name('shop_assignor_records')
//                ->where('order_id', $order_id)
//                //            ->where('single_id', $single_id)
//                ->where('shop_id', $shop_id)
//                ->where('create_time', $create_time)
//                ->setField('is_agree', 2);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage()];
        }
        if ($g_ret !== false) {
            return ['code' => 200, 'message' => '确认成功'];
        }
        return ['code' => 400, 'message' => '确认失败'];
    }

    /**
     * 修复数据 未同意的数据
     */ //327 591
    public function xiufu()
    {
        Db::startTrans();
        try {
            $order = DB::name('shop_assignor_records')->group('create_time')->whereTime('create_time','>','2019-04-28 14:36:44')->where('is_agree', 1)->order('id', 'desc')->select()->toArray(); //查询未同意的所有订单数据

            foreach ($order as $k => &$v) {
                $v['create_time_list'] = DB::name('shop_assignor_records')->group('assignor_content')->where('create_time', $v['create_time'])->select()->toArray();
            } //得到当前同一时间段 不用服务内容的唯一类;

            foreach ($order as $v1) {
                foreach ($v1['create_time_list'] as $v2) {
                    if($v1['assignor_content'] != 9999){
                        $g_ret = Db::name('order_gift_count')
                            ->where('order_id', $v2['order_id'])
                            ->where('single_content', $v2['assignor_content'])
                            ->setDec('gift_count', 1);//订单礼包次数
                        if (!$g_ret) { //就是失败 $g_ret != true
                            // abort(404);

                            return ['code' => 200,'message' => $v1['id']];
                            //throw new \think\Exception($v2['id'], 500);
                        }
                    }

                }

            }
            Db::commit();
            return ['code' => 200,'message' => 'ok'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage(),$e->getTrace()];
        }
    }

    /**
     * @param $shop_assignor_records_id
     * @return array
     * 备份的数据
     */
    public function gift_agree_beifen($shop_assignor_records_id)
    {

        $is_agree = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('is_agree');

        if($is_agree == 2){
            return ['code' => 400, 'message' => '已同意过'];
        }
        Db::startTrans();
        try {
            $order_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('order_id');
            $shop_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('shop_id');
            $create_time = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('create_time');
            $select = Db::name('shop_assignor_records')
                ->where('order_id', $order_id)
                ->where('shop_id', $shop_id)
                ->where('create_time', $create_time)
                ->group('assignor_content')
                ->select();
            //循环减当前类的次数
            foreach ($select as $v) {
                $g_ret = Db::name('order_gift_count')
                    ->where('order_id', $order_id)
                    ->where('single_content', $v['assignor_content'])
                    ->setDec('gift_count', 1);//订单礼包次数
            }
            //$single_id = Db::name('shop_assignor_records')->where('id', $shop_assignor_records_id)->value('single_id');
            //$r = Db::name('order_gift_count')->where('order_id', $order_id)->where('single_id', $single_id)->setDec('gift_count');//订单礼包次数
            //后修改 当前门店 当前时间 当前订单 当前分类 都 修改
            $ret = Db::name('shop_assignor_records')
                ->where('order_id', $order_id)
                //            ->where('single_id', $single_id)
                ->where('shop_id', $shop_id)
                ->where('create_time', $create_time)
                ->setField('is_agree', 2);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage()];
        }
        if ($ret !== false) {
            return ['code' => 200, 'message' => '确认成功'];
        }
        return ['code' => 400, 'message' => '确认失败'];
    }

    /**
     * @param $shop_assignor_records_id
     * @return array
     * 会员卡前台同意生效 以前
     */
    public function member_card_agree2($shop_assignor_records_id)
    {
        $create_time = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('create_time');
        $single_id = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('single_id');
        $shop_id = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('shop_id');
        $card_id = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('card_id');
        $card_type = Db::name('member_card_records')->where('id', $shop_assignor_records_id)->value('card_type');
        if($card_type == 2){
            Db::name('member_card')->where('id', $card_id)->setDec('minus_count');//次卡减去次数
            Db::name('member_card_records')
                ->where('single_id', $single_id)
                ->where('shop_id', $shop_id)
                ->where('create_time', $create_time)->setDec('minus_count');
        }
        //后修改 当前门店 当前时间 当前订单 当前分类 都 修改
        $ret = Db::name('member_card_records')
            ->where('single_id', $single_id)
            ->where('shop_id', $shop_id)
            ->where('create_time', $create_time)
            ->setField('is_agree', 2);
        if ($ret !== false) {
            //$shop_assignor_records[$i]['minus_count'] = $minus_count - 1;
            //Db::name('member_card')->where('id', $param['card_id'])->setDec('minus_count');//次卡减去次数
            return ['code' => 200, 'message' => '确认成功'];
        }
        return ['code' => 400, 'message' => '确认失败'];
    }



    /**
     * @param $order_number
     * @param $member_id
     * @param int $bill
     * @param string $bill_type
     * @param int $bill_header
     * @param string $bill_content
     * @param string $bill_email
     * @param string $bill_phone
     * @param string $explain
     * @param int $shipping
     * @param int $pay_way
     * @return array
     * 服务类订单修改
     */


    public function service_update1($coupon_id = 0 ,$order_number,$member_id,$bill = 2,$bill_type = '',$bill_header = 0,$bill_content = '',$bill_email = '',$bill_phone = '',$explain = '',$shipping = 1,$pay_way = 1)
    {
        if($coupon_id == 0){
            $order = Db::name('order')->where('order_number',$order_number)->where('member_id',$member_id)->find();
            $data = [
                'coupon_id' => 0,
                'coupon_title' => '',
                'coupon_content' => '',
                'coupon_price' => '',
                'coupon_totol_price' => '',
                'amount' => $order['order_price'],
                'bill' => $bill,//是否开发票
                'bill_type' => $bill_type,//是否开发票
                'bill_header' => $bill_header,//是否开发票
                'bill_content' => $bill_content,//是否开发票
                'bill_email' => $bill_email,//是否开发票
                'bill_phone' => $bill_phone,
                'explain' => $explain,
            ];
            $ret = Db::name('order')->where('order_number',$order_number)->where('member_id',$member_id)->update($data);
            if($ret !== false){
                return ['code' => 200 , 'message' => '优惠券取消成功'];
            }
            return ['code' => 0 , 'message' => '失败'];
        }else{
            $member_coupon = Db::name('member_coupon')->where('id',$coupon_id)->find();
            $order = Db::name('order')->where('order_number',$order_number)->where('member_id',$member_id)->find();
            $price = $order['amount'] - $member_coupon['minus_price'];
            if($price == 0){
                $price = 0.01;
            }
            $data = [
                'coupon_id' => $member_coupon['coupon_id'],
                'coupon_title' => $member_coupon['title'],
                'coupon_content' => $member_coupon['content'],
                'coupon_price' => $member_coupon['minus_price'],
                'coupon_totol_price' => $member_coupon['total_price'],
                'amount' => $price,
                'bill' => $bill,//是否开发票
                'bill_type' => $bill_type,//是否开发票
                'bill_header' => $bill_header,//是否开发票
                'bill_content' => $bill_content,//是否开发票
                'bill_email' => $bill_email,//是否开发票
                'bill_phone' => $bill_phone,
                'explain' => $explain,
            ];
            $ret = Db::name('order')->where('order_number',$order_number)->where('member_id',$member_id)->update($data);
            if($ret !== false){
                return ['code' => 200 , 'message' => '优惠券选择成功'];
            }
            return ['code' => 0 , 'message' => '失败'];
        }
    }

    /**
     * @param $yuyue_start_time
     * @param $yuyue_end_time
     * @param $order_number
     * @param $member_id
     * @param int $bill
     * @param string $bill_type
     * @param int $bill_header
     * @param string $bill_content
     * @param string $bill_email
     * @param string $bill_phone
     * @param string $explain
     * @param int $shipping
     * @param int $pay_way
     * @return array 服务修改
     */
    public function service_update($yuyue_start_time, $yuyue_end_time, $order_number, $member_id, $bill = 0, $bill_type = '', $bill_header = 0, $bill_content = '', $bill_email = '', $bill_phone = '', $explain = '', $shipping = 1, $pay_way = 1)
    {
        $data = [
            'bill' => $bill,//是否开发票
            'bill_type' => $bill_type,//是否开发票
            'bill_header' => $bill_header,//是否开发票
            'bill_content' => $bill_content,//是否开发票
            'bill_email' => $bill_email,//是否开发票
            'bill_phone' => $bill_phone,
            'explain' => $explain,
            'yuyue_start_time' => $yuyue_start_time,
            'yuyue_end_time' => $yuyue_end_time

        ];
        foreach ($data as $k => $v) {
            if (empty($v)) unset($data[$k]);
        }
        $ret = Db::name('order')->where('order_number', $order_number)->where('member_id', $member_id)->update($data);
        if ($ret !== false) {
            return ['code' => 200, 'message' => '修改成功', 'save_data' => $data];
        }
        return ['code' => 0, 'message' => '失败'];
    }

    public function service_update2($yuyue_start_time,$yuyue_end_time,$order_number,$member_id,$bill = 2,$bill_type = '',$bill_header = 0,$bill_content = '',$bill_email = '',$bill_phone = '',$explain = '',$shipping = 1,$pay_way = 1)
    {
        $data = [
            'bill' => $bill,//是否开发票
            'bill_type' => $bill_type,//是否开发票
            'bill_header' => $bill_header,//是否开发票
            'bill_content' => $bill_content,//是否开发票
            'bill_email' => $bill_email,//是否开发票
            'bill_phone' => $bill_phone,
            'explain' => $explain,
            'yuyue_start_time' => $yuyue_start_time,
            'yuyue_end_time' => $yuyue_end_time

        ];
        $data_time = [];
        $ret = Db::name('order')->where('order_number',$order_number)->where('member_id',$member_id)->update($data);
        if($ret !== false){
            return ['code' => 200 , 'message' => '修改成功'];
        }
        return ['code' => 0 , 'message' => '失败'];
    }

    /**
     * @param $order_number
     * @param $member_id
     * @param $coupon_id
     * @return array 优惠券金额
     */
    public function service_update_coupon($order_number,$member_id,$coupon_id = 0)
    {
        if($coupon_id == 0){
            $order = Db::name('order')->where('order_number|shop_order_number',$order_number)->where('member_id',$member_id)->find();
            $data = [
                'coupon_id' => 0,
                'coupon_title' => '',
                'coupon_content' => '',
                'coupon_price' => '',
                'coupon_totol_price' => '',
                'amount' => $order['order_price']
            ];
            $ret = Db::name('order')->where('order_number|shop_order_number',$order_number)->where('member_id',$member_id)->update($data);
            if($ret !== false){
                Db::name('member_coupon')->where('id',$order['coupon_id'])->setField('status',1);
                return ['code' => 200 , 'message' => '优惠券取消成功'];
            }
            return ['code' => 0 , 'message' => '失败'];
        }else{
            $member_coupon = Db::name('member_coupon')->where('id',$coupon_id)->find();
            $order = Db::name('order')->where('order_number|shop_order_number',$order_number)->where('member_id',$member_id)->find();
            $price = $order['amount'] - $member_coupon['minus_price'];
            if($price <= 0){
                $price = 0.01;
            }
            $data = [
                'coupon_id' => $coupon_id,
                'coupon_title' => $member_coupon['title'],
                'coupon_content' => $member_coupon['content'],
                'coupon_price' => $member_coupon['minus_price'],
                'coupon_totol_price' => $member_coupon['total_price'],
                'amount' => $price
            ];
            $ret = Db::name('order')->where('order_number|shop_order_number',$order_number)->where('member_id',$member_id)->update($data);
            if($ret !== false){
                Db::name('member_coupon')->where('id',$coupon_id)->setField('status',2);
                return ['code' => 200 , 'message' => '优惠券选择成功'];
            }
            return ['code' => 0 , 'message' => '失败'];
        }

    }

    public function cancel_reason(){
        $data = Db::name('cancel_reason')->field('sort',true)->select();
        return ['code' => 200,'message' => '取消订单理由','data' => $data];
    }

    /**
     * @param string $data
     * 支付库存
     */
    public function kucun($data = '201906031450312498808323'){
        $order_goods = Db::name('order_goods')->where(['order_number|shop_order_number' => $data])->select();

        if(!empty($order_goods)){

            foreach ($order_goods as $k1 => $v1) {
                // 减少属性表库存
                $inventory_attr = Db::name('shop_spec_attr')->where('shop_id',$v1['shop_id'])->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->value('inventory');
                if ($inventory_attr) {
                    if ($inventory_attr < $v1['number']) {
                        //return ['code' => 401, 'message' => $v1['title'] . '(' . $v1['attribute'] . '),库存不足'];
                    }
                    Db::name('shop_spec_attr')->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->where('shop_id',$v1['shop_id'])->setDec('inventory',$v1['number']);//门店库存
                    Db::name('shop_goods')->where('id',$v1['goods_id'])->setInc('sale_number',$v1['number']);//商品销量
                    Db::name('shop')->where('id',$v1['shop_id'])->setInc('sale_number',$v1['number']);//门店销量
                } //先验证
            }

        }
    }

    /**
     * @param int $order_id
     * 退款
     */
    public function tuikuan($order_id = 1897){
        $order_goodstuikuan = Db::name('order_goods')->where('order_id',$order_id)->select();
        if (!empty($order_goodstuikuan)) {
            foreach ($order_goodstuikuan as $k => $v) {
                Db::name('shop_spec_attr')->where(['attribute' => $v['attribute'], 'goods_id' => $v['goods_id']])->where('shop_id', $v['shop_id'])->setInc('inventory', $v['number']);
                $sale_number = Db::name('shop_goods')->where('id', $v['goods_id'])->value('sale_number');
                if ($sale_number >= $v['number']) {
                    Db::name('shop_goods')->where('id', $v['goods_id'])->setDec('sale_number', $v['number']);
                }
            }
        }
    }
    public function tuikuan2($id = 1897,$status,$fail_explain){

        Db::startTrans();
        try {
            $find = Db::name('order')->find($id);
            $refund_ret = Db::name('order_refund')->where('order_id', $id)->update(['fail_explain' => $fail_explain]);
            if ($status == 11) {
                $message = $status == 11 ? '退款成功' : '退出失败';


                $refund = Db::name('order_refund ors')->field('ors.*,rr.title')->where('order_id', $find['id'])->join('refund_reason rr', 'ors.reason_id = rr.id','right')->find();


                if ($find['pay_type'] == '2') {
                    $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '', $find['shop_order_number']);

                    if ($ret->code !== '10000') {
                        return ['code' => 400, 'message' => $ret->sub_msg];
                        //fail($ret->sub_msg);
                    }
                    //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);
                } else if ($find['pay_type'] == '1') {
                    //halt('wx');
                    $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);

                    //halt($ret);
                    if ($ret['result_code'] == 'FAIL') {
                        return ['code' => 400, 'message' => $ret['err_code_des']];
                        //fail($ret['err_code_des']);
                    }

                }
                $order_goodstuikuan = Db::name('order_goods')->where('order_id', $find['id'])->select();
                if (!empty($order_goodstuikuan)) {
                    foreach ($order_goodstuikuan as $k => $v) {
                        Db::name('shop_spec_attr')->where(['attribute' => $v['attribute'], 'goods_id' => $v['goods_id']])->where('shop_id', $v['shop_id'])->setInc('inventory', $v['number']);
                        $sale_number = Db::name('shop_goods')->where('id', $v['goods_id'])->value('sale_number');
                        if ($sale_number >= $v['number']) {
                            Db::name('shop_goods')->where('id', $v['goods_id'])->setDec('sale_number', $v['number']);
                        }
                    }
                }

                $ret = Db::name('order')->where('id', $id)->update(['status' => input('post.status')]);				$ret1 = Db::name('order')->where('id', $id)->update(['tui_status' => 3]);

                Db::commit();
                return ['code' => 200, 'message' => '退款成功'];
            } else {
                //$ret = Db::name('order')->where('id', $id)->update(['status' => input('post.status')]);
				$ret = Db::name('order')->where('id', $id)->update(['status' => 5]);				$ret1 = Db::name('order')->where('id', $id)->update(['tui_status' => 2]);
                Db::commit();
                return ['code' => 200, 'message' => '退款失败'];
            }
        } catch (Exception $e) {
            Db::rollback();
            return ['code' => 400, 'message' => '退款失败'];
        }

    }

    /**
     * @param $order_number
     * @return array
     * 当前订单方法
     */
    public function now_order_inventory($order_number){
        $order_goods = Db::name('order_goods')->where(['order_number|shop_order_number' => $order_number])->select();

        if(!empty($order_goods)){
            foreach ($order_goods as $k1 => $v1) {
                // 减少属性表库存
                $inventory_attr = Db::name('shop_spec_attr')->where('shop_id',$v1['shop_id'])->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->value('inventory');
                if ($inventory_attr) {
                    if ($inventory_attr < $v1['number']) {
                        return ['code' => 401, 'message' => $v1['title'] . '(' . $v1['attribute'] . '),库存不足'];
                    }
                    //Db::name('shop_spec_attr')->where(['attribute' => $v1['attribute'], 'goods_id' => $v1['goods_id']])->where('shop_id',$v1['shop_id'])->setDec('inventory',$v1['number']);//门店库存
                    //Db::name('shop_goods')->where('id',$v1['goods_id'])->setInc('sale_number',$v1['number']);//商品销量
                    // Db::name('shop')->where('id',$v1['shop_id'])->setInc('sale_number',$v1['number']);//门店销量
                } //先验证
            }
            return ['code' => 200, 'message' => '可以支付'];
        }
    }

    /**
     * @param $member_id
     * @param $order_id
     * @return array
     * 取消订单
     */
    public function cancel($member_id, $order_id,$cancel_content = '后台手动取消')
    {

        try {

            writeLog($member_id . '-' . $order_id);
            $order = Db::name('order')->where('member_id', $member_id)->where('id|shop_order_number', $order_id)->find();
            if (empty($order)) {
                return ['code' => 400, 'message' => '条件不符合'];
            }
            if(!in_array($order['status'],[1,50])){ //等待沟通和待支付
                return ['code' => 400, 'message' => '此状态订单不能取消'];
            }
            if ($order['order_type'] == 1) { //商品
                $order_goods = Db::name('order_goods')->where('order_id', $order['id'])->select();
                if (!empty($order_goods)) {
                    foreach ($order_goods as $k => $v) {
//                        Db::name('shop_spec_attr')->where(['attribute' => $v['attribute'], 'goods_id' => $v['goods_id']])->where('shop_id', $v['shop_id'])->setInc('inventory', $v['number']);
//                        $sale_number = Db::name('shop_goods')->where('id', $v['goods_id'])->value('sale_number');
//                        if($sale_number >= $v['number'])
//                        {
//                            Db::name('shop_goods')->where('id', $v['goods_id'])->setDec('sale_number', $v['number']);
//                        }
                    }
                }
            }
            if ($order['order_type'] == 3) { //礼包
                if ($order['t_type'] == 1) { //1礼包 2会员卡
                    $sale_number = Db::name('gift')->where('id', $order['gift_id'])->value('sale_number');
                    if ($sale_number >= 1) {
                        Db::name('gift')->where('id', $order['gift_id'])->setDec('sale_number', 1);
                        Db::name('gift')->where('id', $order['gift_id'])->setInc('sycs', 1);//销量减去 次数加上

                    }
                }
                if($order['t_type'] == 2){
                    //$sale_number = Db::name('system_card')->where('id', $order['system_card_id'])->value('sale_number');
                    Db::name('system_card')->where('id', $order['system_card_id'])->setDec('sale_number',1);//销量
                    Db::name('system_card')->where('id', $order['system_card_id'])->setInc('sycs', 1);

                }


            }
            $service_order = Db::name('service_order_goods')->where('order_id',$order_id)->select();

            if(!empty($service_order)){
                foreach ($service_order as $vs){
                    Db::name('shop_spec_attr')->where('id', $vs['spec_id'])->setInc('inventory', $vs['number']);
                }
            }


            //必须先处理完毕 才能够在删除
            $ret = Db::name('order')->where('member_id', $member_id)->where('id', $order_id)->update(['status' => 20,'cancel_content' => $cancel_content]);
            if ($ret) {
                if(!empty($order['coupon_id'])){ //一个会员2个优惠券
                    Db::name('member_coupon')->where('member_id',$order['member_id'])->where('id',$order['coupon_id'])->setField('status',1);
                }
                return ['code' => 200, 'message' => '取消订单成功'];
            }
            return ['code' => 400, 'message' => '失败'];
        } catch (\Exception $e) {
            return ['code' => $e->getCode(), 'message' => $e->getMessage(), 'line' => $e->getLine()];
        }
    }

    /**
     * 退款分类
     * @return array
     */
    public function refund_reason(){
        $data = Db::name('refund_reason')->field('sort',true)->select();
        return ['code' => 200,'message' => '信息','data' => $data];
    }

    /**
     * 退款申请 服务和礼包
     * @param $member_id
     * @param $order_id
     * @param $type
     * @param $reason_id
     * @param $explain
     * @return array
     */
    public function refund($member_id,$order_number,$type = 1,$reason_id,$explain){
        Db::startTrans();
        try {
            $type = 1;//1退款
            $order_refund_id = 0;
            $order = Db::name('order')->where('order_number',$order_number)->find();

            if($order['is_free_order'] == 2){
                return ['code' => 400,'message' => '免费订单不允许退款'];
            }
            $gift_card = Db::name('gift_card')->where('order_number',$order['order_number'])->find();
            if(!empty($gift_card)){
                return ['code' => 400,'message' => '激活卡礼包不支持退款'];
            }
            if(time() - strtotime($order['pay_time']) > 7*3600*24){
                return ['code' => 400,'message' => '已付款超过7天订单无法退款'];
            }
            if($order['refund_status'] == 2){
                return ['code' => 400,'message' => '退款审核失败,不能再退款'];
            }


            $data = [
                'member_id' => $member_id,
                'order_number' => $order_number,
                'order_type' => $order['order_type'],
                'shop_order_number' => $order['shop_order_number'],
                'type' => $type,
                'reason_id' => $reason_id,
                'explain' => $explain,
                'order_id' => $order['id'],
                'amount' => $order['amount'],
                'create_time' => date('Y-m-d H:i:s',time())
            ];
            $ret = Db::name('order')->where('order_number',$order_number)->setField('status', 10);

            if ($ret !== false) {
                $order_refund_id = Db::name('order_refund')->insertGetId($data);
            }
            Db::commit();
            if($order_refund_id){
                if($type == 1){
                    return ['code' => 200,'message' => '退款单申请成功'];
                }
                if($type == 2){
                    return ['code' => 200,'message' => '退货单申请成功'];
                }

            }


        } catch (Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
        }
    }

    /**
     * @param $member_id
     * @param $order_number
     * @param int $type
     * @param $reason_id
     * @param $explain
     * @param $goods_id
     * @return array
     * 退货退款商品类
     */
    public function refund_goods($member_id,$order_number,$type = 2,$reason_id,$explain,$goods_id){
        Db::startTrans();
        try {
            $order_refund_id = 0;
            $order = Db::name('order')->where('order_number|shop_order_number',$order_number)->find();
            if($order['order_number'] != $order['shop_order_number']){
                //return ['code' => 0,'message' => '商品总单号号和店铺单号不同'];
            }
            if($order['status'] == 2){
                $type = 1;
            }
            if($order['status'] == 3 || $order['status'] == 4){
                $type = 2;
            }
            if($order['is_free_order'] == 2){
                return ['code' => 400,'message' => '免费订单不允许退款'];
            }
            if(time() - strtotime($order['pay_time']) > 7*3600*24){
                return ['code' => 400,'message' => '已付款超过7天订单无法退款'];
            }
            if($order['order_type'] != 1){
                return ['code' =>0,'message' => '只有商品订单才可以退货'];
            }
            $goods_price = Db::name('order_goods')->where('order_id',$order['id'])->where('goods_id',$goods_id)->value('price');
            $number = Db::name('order_goods')->where('order_id',$order['id'])->where('goods_id',$goods_id)->value('number');
            $amount = $goods_price*$number;

            $data = [
                'goods_id' => $goods_id,
                'member_id' => $member_id,
                'order_number' => $order_number,
                'order_type' => $order['order_type'],
                'shop_order_number' => $order['shop_order_number'],
                'type' => $type,
                'reason_id' => $reason_id,
                'explain' => $explain,
                'order_id' => $order['id'],
                'amount' => $amount,
                'create_time' => date('Y-m-d H:i:s',time())
            ];
            $ret = Db::name('order')->where('order_number',$order_number)->setField('status', 10);

            if ($ret !== false) {
                $order_refund_id = Db::name('order_refund')->insertGetId($data);
            }
            Db::commit();
            if($order_refund_id){
                if($type == 1){
                    return ['code' => 200,'message' => '退货款申请成功'];
                }
                if($type == 2){
                    return ['code' => 200,'message' => '退货款申请成功'];
                }

            }


        } catch (Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
        }
    }


    /**
     * @param $shop_order_number
     * @param $member_id
     * 商品订单确认收货
     */
    public function confirm_goods($shop_order_number, $member_id)
    {
        $ret = Db::name('order')->where('order_type', 1)->where('shop_order_number', $shop_order_number)->where('status', 3)->where('member_id', $member_id)->setField('status', '5');
        if ($ret !== false) {
            return ['code' => 200, 'message' => '确认收货成功'];
        }
    }


    public function delete($id)
    {
        $ret = Db::name('order')->where('id', $id)->setField('status', 0);
    }




    public function encrypt(){
        $data = '{
    "code":200,
    "message":"确认订单展示页",
    "member_address":{
        "id":440,
        "member_id":1,
        "name":"1",
        "phone":"1",
        "area_id":null,
        "area_info":"1",
        "address":"1",
        "status":1,
        "create_time":2018
    },
    "radio_cart_list":[
        {
            "member_id":1,
            "shop_id":2,
            "shop_title":"牡丹江汽车服务1",
            "list":[
                {
                    "member_id":1,
                    "cart_id":2,
                    "goods_id":1462,
                    "shop_id":2,
                    "number":11,
                    "price":"399.00",
                    "attribute":"X,门店2",
                    "shop_title":"牡丹江汽车服务1",
                    "title":"黑楸久酒",
                    "thumb_img":"uploads/20180909/cae695bedfb648ce8d8367407185ca32.jpg",
                    "freight":"2.00"
                },
                {
                    "member_id":1,
                    "cart_id":7,
                    "goods_id":1462,
                    "shop_id":2,
                    "number":7,
                    "price":"399.00",
                    "attribute":"X,门店2",
                    "shop_title":"牡丹江汽车服务1",
                    "title":"黑楸久酒",
                    "thumb_img":"uploads/20180909/cae695bedfb648ce8d8367407185ca32.jpg",
                    "freight":"2.00"
                }
            ],
            "shop_freight":4,
            "shop_count":18,
            "shop_goods_amount":7182,
            "shop_amount":7186,
            "explain":""
        }
    ],
    "order_amount":7186,
    "order_others":{
        "shipping":"1",
        "pay_way":"1",
        "bill":"1",
        "bill_data":{
            "bill_type":1,
            "bill_header":1,
            "bill_content":"发票内容",
            "bill_phone":"15245016157",
            "bill_email":"15245016157@163.com"
        }
    }
}';

        $data = json_decode($data,true);
        $config = $this->rsa_config['request_pubKey'];

        $data = $this->response_encrypt($data,$config);
        return ($data);

    }

    /**
     * des-ecb解密
     * @param string  $data 加密数据
     * @param string  $key 加密密钥
     */
    public function decrypt (){
        return $this->request();
    }

    public function is_hidden($id){
        $ret = Db::name('shop_assignor_records')->where('id','in',$id)->update(['is_show' => 2,'hidden_time' => date('Y-m-d H:i:s')]);
        if($ret){
            return ['code' =>200,'message' =>'隐藏成功'];

        }
        return ['code' =>400,'message' => '失败'];
    }











}